using System;
using System.Collections;
using System.Collections.Generic;
using GardeningSkeleton.Platform.SkillSpace;
using UnityEngine;

namespace Platform.Skill.Core
{
    public class DeployerConfigFactory : MonoBehaviour
    {
        // 一个字典用以缓存类型
        private static Dictionary<string, Type> typeCache = new Dictionary<string, Type>();

        public static ISkillSelector CreateSkillSelector(SkillData data) // 范围选择算法 
        {
            if (data.selectorType == SelectorTypeEnum.None) return null;

            // 根据选择类型构建类名
            string className = string.Format("GardeningSkeleton.Platform.SkillSpace.{0}SkillSelector", data.selectorType.ToString());
            return CreateObject<ISkillSelector>(className);
        }

        public static BaseImpactEffect[] CreateImpactEffects(SkillData data) // 效果算法
        {
            BaseImpactEffect[] impacts = new BaseImpactEffect[data.impactType.Length];
            for (int i = 0; i < data.impactType.Length; i++)
            {
                string className = string.Format("GardeningSkeleton.Platform.SkillSpace.{0}Impact", data.impactType[i].ToString());
                impacts[i] = CreateObject<BaseImpactEffect>(className);
            }
            return impacts;
        }

        private static T CreateObject<T>(string className) where T : class // 创建对应算法
        {
            if (!typeCache.TryGetValue(className, out Type type))
            {
                // 如果缓存中没有该类型，反射获取并缓存
                type = Type.GetType(className);
                if (type != null)
                {
                    typeCache[className] = type;
                }
                else
                {
                    Debug.LogError($"Type {className} not found.");
                    return null;
                }
            }

            return Activator.CreateInstance(type) as T;
        }
    }
}